package com.ld.shieldsb.common.core.util;

import com.github.zafarkhaja.semver.Version;

/**
 * 版本号判断工具,参考https://github.com/gorkem/java-semver
 * 
 * @ClassName VersionUtil
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2018年12月10日 上午9:58:06
 *
 */
public class VersionUtil {
    public static final int V_BIG = 1;
    public static final int V_SMALL = -1;
    public static final int V_EQUAL = 0;

    /**
     * 版本号1是否比版本号2大，结果1大-1小，0相等<br>
     * 语言版本号可能包含5部分主版本号.次版本号.修订号-预发布版本号+构建号 <br>
     * 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < <br>
     * 1.0.0-rc.1 < 1.0.0 <br>
     * <br>
     * 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0-rc.1+build.1 < 1.0.0 < 1.0.0+0.3.7 < 1.3.7+build <br>
     * < 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a.
     * 
     * @Title isBigVersion
     * @author 吕凯
     * @date 2018年12月10日 上午9:59:59
     * @param version1
     * @param version2
     * @return boolean
     */
    public static int compareVersion(String version1, String version2) {
        //
        if (version1 != null && version2 != null) {
            Version v1 = Version.valueOf(version1);
            Version v2 = Version.valueOf(version2);
//            int compV = v1.compareTo(v2); //不比较构建号
            int compV = Version.BUILD_AWARE_ORDER.compare(v1, v2);
            if (compV > V_BIG) {
                compV = V_BIG;
            } else if (compV < V_SMALL) {
                compV = V_SMALL;
            }
            return compV;
        } else if (version1 == null && version2 == null) {
            return V_EQUAL;
        } else if (version2 == null) {
            return V_BIG;
        }
        return V_SMALL;
    }

    /**
     * 判断version1是否比version2小
     * 
     * @Title lessThan
     * @author 吕凯
     * @date 2018年12月10日 上午11:34:11
     * @param version1
     * @param version2
     * @return boolean
     */
    public static boolean lessThan(String version1, String version2) {
        return compareVersion(version1, version2) == V_SMALL;
    }

    public static void main(String[] args) {
        // 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0-rc.1+build.1 < 1.0.0 < 1.0.0+0.3.7 < 1.3.7+build
        // < 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a.
//        String v1 = "1.0.0-alpha";
//        String v2 = "1.0.0-alpha.1";
//        String v3 = "1.0.0-beta.2";
//        String v4 = "1.0.0-beta.11";
//        String v5 = "1.0.0-rc.1";
//        String v6 = "1.0.0-rc.1+build.1";
//        String v7 = "1.0.0";
//        String v8 = "1.0.0+0.3.7";
//        String v9 = "1.3.7+build";
//        String v10 = "1.3.7+build.2.b8f12d7";
//        String v11 = "1.3.7+build.11.e0f985a";
//        System.out.println(compareVersion(v2, v1));
//        System.out.println(compareVersion(v3, v2));
//        System.out.println(compareVersion(v4, v3));
//        System.out.println(compareVersion(v5, v4));
//        System.out.println(compareVersion(v6, v5));
//        System.out.println(compareVersion(v7, v6));
//        System.out.println(compareVersion(v8, v7));
//        System.out.println(compareVersion(v9, v8));
//        System.out.println(compareVersion(v10, v9));
//        System.out.println(compareVersion(v11, v10));
//
//        System.out.println(lessThan(v7, v8));
//
//        System.out.println("=====================================");
//        Version v = Version.valueOf("1.0.0-rc.1+build.1");
//
//        int major = v.getMajorVersion(); // 1
//        int minor = v.getMinorVersion(); // 0
//        int patch = v.getPatchVersion(); // 0
//
//        String normal = v.getNormalVersion(); // "1.0.0"
//        String preRelease = v.getPreReleaseVersion(); // "rc.1"
//        String build = v.getBuildMetadata(); // "build.1"
//
//        String str = v.toString(); // "1.0.0-rc.1+build.1"
//
//        System.out.println("normal:" + normal);
//        System.out.println("preRelease:" + preRelease);
//        System.out.println("build:" + build);
//
//        Version vv1 = Version.forIntegers(1);
//        Version vv2 = Version.forIntegers(1, 2);
//        Version vv3 = Version.forIntegers(1, 2, 3);
//
//        System.out.println("vv1:" + vv1);
//        System.out.println("vv2:" + vv2);
//        System.out.println("vv3:" + vv3);
//
//        Version vvv1 = Version.valueOf("1.0.0-rc.1+build.1");
//        Version vvv2 = Version.valueOf("1.3.7+build.2.b8f12d7");
//
//        int result = vvv1.compareTo(vvv2); // < 0
//        System.out.println("vvv1-vvv2:" + result);
    }

}
